package com.zt.controller;

import com.zt.pojo.Goods;
import com.zt.service.GoodsService;
import com.zt.utils.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 王佳祥
 * @Date: 2024 09 04 10:52
 */
@RestController
@Slf4j
@Api(tags = "goods",description = "对controller的描述")
public class GoodsController {

    @Autowired
    private GoodsService service;

    @Autowired
    private RestHighLevelClient restHighLevelClient;


    @ApiOperation("添加商品并进行审核")
    @PostMapping("/save")
    public ResponseResult save(@ApiParam("goods 参数") @RequestBody Goods goods){
        ResponseResult responseResult = service.saveAndAuto(goods);
        return responseResult;
    }

    @SneakyThrows
    @PostMapping("/search")
    @ApiOperation("使用es进行查询")
    public void list(){
        SearchRequest searchRequest=new SearchRequest("zk3_test");

        SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();

        searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("apSatus",0)));

        searchRequest.source(searchSourceBuilder);

        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = search.getHits();
        for (SearchHit hit : hits) {
            System.err.println(hit.getSourceAsString());
        }
    }
}


